package com.TropicalFlavor.controller;

import com.TropicalFlavor.po.MarketGoods;
import com.TropicalFlavor.po.MarketUser;
import com.TropicalFlavor.service.GoodsService;
import com.TropicalFlavor.tool.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController
{
    @Resource
    private GoodsService goodsService;

    //进入首页、返回首页
    @RequestMapping(value = {"/","/home.do"})
    public String home(HttpServletRequest request, Model model)
    {
        MarketUser marketUser = (MarketUser) request.getSession().getAttribute("marketUser");

        List<MarketGoods> bookList = goodsService.SearchGoods("书");
        List<MarketGoods> showBookList;
        if(bookList.size() >= 5)
            showBookList = bookList.subList(0, 5);
        else
            showBookList = bookList;

        List<MarketGoods> dailyList = goodsService.SearchGoods("日用品");
        List<MarketGoods> showDailyList;
        if(dailyList.size() >= 5)
            showDailyList = dailyList.subList(0, 5);
        else
            showDailyList = dailyList;

        List<MarketGoods> studyList = goodsService.SearchGoods("学习用品");
        List<MarketGoods> showStudyList;

        if(studyList.size() >= 5)
            showStudyList = studyList.subList(0, 5);
        else
            showStudyList = studyList;

        List<MarketGoods> bedList = goodsService.SearchGoods("床上用品");
        List<MarketGoods> showBedList;

        if(bedList.size() >= 5)
            showBedList = bedList.subList(0, 5);
        else
            showBedList = bedList;

        model.addAttribute("bookList", showBookList);
        model.addAttribute("dailyList", showDailyList);
        model.addAttribute("bedList", showBedList);
        model.addAttribute("studyList", showStudyList);

        if(!StringUtils.getInstance().isNullOrEmpty(marketUser))
        {
            model.addAttribute("marketUser", marketUser);
        }
        else
        {
            marketUser = new MarketUser();
            model.addAttribute("marketUser", marketUser);
        }
        try
        {

        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "page/login_page";
        }
        return "index";
    }

    //进入商城
    @RequestMapping(value = "/goToMallPage.do")
    public String mallPage(HttpServletRequest request, Model model)
    {
        MarketUser user = (MarketUser) request.getSession().getAttribute("marketUser");

//        if(StringUtils.getInstance().isNullOrEmpty(user))
//        {
//            return "page/login_page";
//        }
        model.addAttribute("marketUser", user);

        boolean isLastPage = false;

        if(goodsService.getGoodsSum() <= 12)
        {
            isLastPage = true;
        }
        model.addAttribute("isLastPage", isLastPage);

        List<MarketGoods> list = selectTen(0);

        model.addAttribute("marketGoodsList", list);
        model.addAttribute("currentPage", 0);

        return "page/goods";
    }

    //前往首页
    @RequestMapping(value = "/goToFirstPage.do")
    public String goToFirstPage(HttpServletRequest request, Model model)
    {
        MarketUser user = (MarketUser) request.getSession().getAttribute("marketUser");
        List<MarketGoods> list = selectTen(0);

        boolean isLastPage = false;

        if(goodsService.getGoodsSum() <= 12)
        {
            isLastPage = true;
        }

        model.addAttribute("marketUser", user);
        model.addAttribute("isLastPage", isLastPage);

        model.addAttribute("marketGoodsList", list);
        model.addAttribute("currentPage", 0);

        return "page/goods";
    }

    //前往尾页
    @RequestMapping(value = "/goToLastPage.do")
    public String goToLastPage(HttpServletRequest request, Model model)
    {
        MarketUser user = (MarketUser) request.getSession().getAttribute("marketUser");

        Integer sum = goodsService.getGoodsSum();

        Integer page;

        if(sum%12==0)
            page = sum - 12;
        else
            page = sum - sum%12;


        List<MarketGoods> list = selectTen(page);

        boolean isLastPage = true;

        model.addAttribute("isLastPage", isLastPage);
        model.addAttribute("marketUser", user);
        model.addAttribute("marketGoodsList", list);
        model.addAttribute("currentPage", page);

        return "page/goods";
    }

    //翻页
    @RequestMapping(value = "/changePage.do")
    public String changePage(HttpServletRequest request, Model model, @RequestParam String pages)
    {
        MarketUser user = (MarketUser) request.getSession().getAttribute("marketUser");

//        if(StringUtils.getInstance().isNullOrEmpty(user))
//        {
//            return "page/login_page";
//        }
        model.addAttribute("marketUser", user);
        Integer page = Integer.parseInt(pages);

        List<MarketGoods> list = selectTen(page);

        boolean isLastPage = false;

        if(page + 12 >= goodsService.getGoodsSum())
        {
            isLastPage = true;
        }
        //System.out.println(goodsService.getGoodsSum());

        model.addAttribute("isLastPage", isLastPage);

        //System.out.println(page);

        //System.out.println(list);

        model.addAttribute("marketGoodsList", list);
        model.addAttribute("currentPage", page);

        return "page/goods";
    }

    //获取十个商品，用于在商品页面展示
    private List<MarketGoods> selectTen(int start)
    {
        Map map = new HashMap();
        map.put("start", start);
        List<MarketGoods> list = goodsService.selectTenGoods(map);
        System.out.println(list);
        return list;
    }
}
